﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON

GO


CREATE PROCEDURE [dbo].[bhp_temp_AddBlogStructure]
AS
BEGIN

	DECLARE
		@BlogTabId				INT,
		@TabId					INT


	SELECT @BlogTabId = id FROM n_Tree WHERE name = 'BLOG_POSTS' AND typ = 'T' AND del = 0
	
	UPDATE
		bht_Columns
	SET
		Sort = c.Sort * 100
	FROM 
		bht_Columns c
		INNER JOIN n_Tree t ON c.ColumnId = t.id
	WHERE
		parent = @BlogTabId
		AND Sort IS NOT NULL
		
	-- BLOG_POSTS.shortTitle
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'shortTitle', @typ = 2, @siz1 = 100, @siz2 = 1, @user = 900900
	--UPDATE BLOG_POSTS SET shortTitle = ''
	--ALTER TABLE BLOG_POSTS ALTER COLUMN shortTitle nvarchar(100) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Krótki tytuł', N'', 150
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'shortTitle'
	
	-- BLOG_POSTS.shortDescription
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'shortDescription', @typ = 2, @siz1 = 400, @siz2 = 1, @user = 900900
	--UPDATE BLOG_POSTS SET shortDescription = ''
	--ALTER TABLE BLOG_POSTS ALTER COLUMN shortDescription nvarchar(400) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Krótki opis', N'', 550
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'shortDescription'
	

	SELECT @TabId = TabId FROM bht_GroupColumnsSplit WHERE ColumnId = (SELECT TOP 1 id FROM n_Tree WHERE parent = @BlogTabId AND name = 'title')
	
	INSERT INTO bht_GroupColumnsSplit (ColumnId, TabId)
		SELECT id, @TabId FROM n_Tree WHERE name IN ('shortTitle', 'shortDescription') AND parent = @BlogTabId

END
GO

EXEC [bhp_temp_AddBlogStructure]
GO

DROP PROCEDURE [bhp_temp_AddBlogStructure]